package leetcode11;

/**
 * 74. 搜索二维矩阵
 */
public class test60 {
    public boolean searchMatrix(int[][] matrix, int target) {
        int rowLength = matrix[0].length;
        for(int i = 0;i < matrix.length;i++){
            if(matrix[i][0] <= target && matrix[i][rowLength - 1] >= target){
                return find(matrix[i],target);
            }
        }
        return false;
    }
    public boolean find(int[] nums,int target){
        int left = 0;
        int right = nums.length - 1;
        while(left <= right){
            int mid = left + (right - left) / 2;
            if(nums[mid] > target){
                right = mid - 1;
            }else if(nums[mid] < target){
                left = mid + 1;
            }else{
                return true;
            }
        }
        return false;
    }
}
